/**
 * 				MochitoServerRMI.java
 *				------------
 *		
 *   Author:	bin04017@gmail.com
 *	 Created:	22.12.2006
 *	 Project:	Mochito-TransferFramework
 *	 Package:	mochitoConnection.interfaces.server.rmi
 *	 Usage:		mochito server unicast remote object
 * *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */
package mochitoConnection.rmi.server;

import java.rmi.RemoteException;
import java.util.LinkedList;

import mochitoConnection.interfaces.server.MochitoServerConnection;
import mochitoConnection.rmi.MochitoConnectionRMI;
import mochitoTransferFactory.MochitoException;
import mochitoTransferFactory.mochitoHost.MochitoHost;
import mochitoTransferableObject.AbstractMochitoTransferableObject;
import mochitoTransferableObject.MochitoTransferableInfo;
import mochitoTransferableObject.MochitoTransferableObject;

/**
 * @author bin04017
 * 
 */
public class MochitoServerRMI extends MochitoConnectionRMI implements
		MochitoServerConnection {

	private long count = 0;

	private LinkedList<AbstractMochitoTransferableObject> objList = new LinkedList<AbstractMochitoTransferableObject>();

	/**
	 * 
	 */
	private static final long serialVersionUID = -6933523090895289731L;

	public MochitoServerRMI(String host) throws MochitoException,
			RemoteException {
		super(host);
	}

	public MochitoServerRMI(MochitoHost host) throws MochitoException,
			RemoteException {
		super(host);
	}

	public void addMochitoTransferableObject(AbstractMochitoTransferableObject obj)
			throws RemoteException {
		objList.addLast(obj);
		++count;
	}

	public AbstractMochitoTransferableObject getMochitoTransferableObject()
			throws RemoteException {
		--count;
		Object obj = objList.getFirst();
		return (AbstractMochitoTransferableObject) obj;
	}

	public long getCount() throws RemoteException {
		return count;
	}

	public LinkedList<MochitoTransferableInfo> getMochitoTransferInfo() throws RemoteException {
		LinkedList<MochitoTransferableInfo> list = new LinkedList<MochitoTransferableInfo>();
		for(int index = 0; index != this.objList.size(); index++){
			list.add(new MochitoTransferableInfo(this.objList.get(index)));
		}
		return list;
	}

	public MochitoTransferableObject getMochitoTransferableObject(Long id) throws RemoteException {
		for(int index = 0; index != this.objList.size(); index++){
			if(id == this.objList.get(index).getMochitoTransferableObjectId()){
				return (MochitoTransferableObject) this.objList.get(index);
			}
		}	
		return null;
	}

	public MochitoTransferableObject getMochitoTransferableObject(MochitoTransferableInfo info) throws RemoteException {
		return getMochitoTransferableObject(info.getMochitoTransferableObjectId());
	}
}
